import numpy as np
import wave
import pylab as pl

#import wav file
filename = ("test1.wav")
f = wave.open(filename,"rb")

#read the format inofrmation
num_frames = f.getnframes()
num_channels = f.getnchannels()
sample_rate = f.getframerate()

#get waveform data
str_data = f.readframes(num_frames)
f.close

#convert wave data to arrys
wave_data = np.frombuffer(str_data,dtype=np.short)
wave_data.shape = -1,num_channels

wave_data = wave_data.T[0] 
#print(wave_data.shape)
time = np.arange(0 ,num_frames) * (1.0/sample_rate)
time = np.array(time)

#pre-emphasis module
signal =  wave_data
emp_factor = 0.9
emphasized_signal1 = np.array(signal[1:]) - (emp_factor) * np.array(signal[:-1])
emphasized_signal = np.append(signal[0],emphasized_signal1)
#emphasized_signal = emphasized_signal[0]


#framing module
# total_frame = 122868 sample_rate = 22050
# choose frame_size = 0.025 = 25ms and frame_stride = 0.01 = 10ms 
frame_size = 0.025
frame_stride = 0.01
frame_length = round(frame_size * sample_rate) 
frame_step = round(frame_stride * sample_rate)
signal_length = num_frames
total_interval = int(np.ceil(float(np.abs(signal_length - frame_length)) / frame_step)) #total intervals of frame obtained 
#generate maximum frame index
max_index = round((signal_length - frame_length)/ frame_step) 

#slice the signal & time into frames
signal_frame = np.zeros((max_index,frame_length)) #initial a (total_interval,frame_length)array
for n in range(0,max_index):
    signal_frame[n] = np.array(signal[n*frame_step:n*frame_step + frame_length])

time_frame = np.zeros((max_index,frame_length))
for n in range(0,max_index):
    time_frame[n] = np.array(time[n*frame_step: n*frame_step + frame_length])

#windowing module
signal_frame_windowing = np.zeros((max_index,frame_length))
for n in range(0,max_index):
    signal_frame_windowing[n] = np.array(signal_frame[n] * np.hamming(frame_length))


if __name__ == "__main__":
    
    #plot the waveform
    pl.subplot(221)
    pl.title("input Waveform ")
    pl.plot(time, wave_data)


    pl.subplot(222)
    pl.title("Pre Emphasiszed Waveform")
    pl.plot(time, emphasized_signal)

    pl.subplot(223)
    pl.title("framing sample")
    pl.plot(time_frame[10], signal_frame[10])

    pl.subplot(224)
    pl.title("windowing sample")
    pl.plot(time_frame[10], signal_frame_windowing[10])


    pl.show()